package cn.myfirefly.moming.house.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import cn.myfirefly.moming.common.controller.BaseCommonController;
import cn.myfirefly.moming.common.service.SystemConfigService;
import cn.myfirefly.moming.house.entity.SpfTradeContractWebimage;
import cn.myfirefly.moming.house.model.SpfTradeContractDetailDto;
import cn.myfirefly.moming.house.model.SpfTradeContractDto;
import cn.myfirefly.moming.house.model.SpfTradeContractWebimageDto;
import cn.myfirefly.moming.house.model.SpfTradeTrackFormDto;
import cn.myfirefly.moming.house.model.SpfTradeTrackFormFilterDto;
import cn.myfirefly.moming.house.model.SpfTradeWageDto;
import cn.myfirefly.moming.house.service.SpfTradeContractService;
import cn.myfirefly.moming.house.service.SpfTradeContractWebimageService;
import cn.myfirefly.moming.house.service.SpfTradeTrackFormService;
import cn.myfirefly.moming.house.service.SpfTradeWageService;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.model.ResultDto;
import cn.myfirefly.utils.FileTransportUtil;
import cn.myfirefly.utils.FileUtil;
import cn.myfirefly.utils.FilterUtil;
import cn.myfirefly.utils.HtmlTemplateUtil;
import cn.myfirefly.utils.MailUtil;
import cn.myfirefly.utils.PropUtil;
import cn.myfirefly.utils.StrUtil;

@Controller
@RequestMapping("spfTradeContractWebimage")
public class SpfTradeContractWebimageController extends BaseCommonController {

	@Autowired
	private SpfTradeContractWebimageService spfTradeContractWebimageService;
	
	@Autowired
	private SpfTradeTrackFormService spfTradeTrackFormService;
	
	@Autowired
	private SpfTradeContractService spfTradeContractService;
	
	@Autowired
	private SystemConfigService systemConfigService;
	
	@Autowired
	private SpfTradeWageService spfTradeWageService;
	
	@RequestMapping("index.do")
	public String index(HttpServletRequest request, 
			HttpServletResponse response) {
		return "spftradecontractwebimage/index";
	}
	
	@RequestMapping("imgindex.do")
	public String imgindex(HttpServletRequest request, 
			HttpServletResponse response) {
		String contractId = request.getParameter("contractId");
		request.setAttribute("contractId", contractId);
		SpfTradeContractDto spfTradeContractDto = spfTradeContractService.getSpfTradeContract(new Integer(contractId));
		if (StrUtil.equals(spfTradeContractDto.getTradeContractStatus(), "5") || 
				StrUtil.equals(spfTradeContractDto.getTradeContractStatus(), "6") ) {
			request.setAttribute("noupdate", "true");
		} else {
			request.setAttribute("noupdate", "false");
		}
		return "spftradecontractwebimage/imgindex";
	}
	
	@RequestMapping("uploadIndex.do")
	public String uploadIndex(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("contractId", request.getParameter("contractId"));
		request.setAttribute("uploadIndex", "0");
		return "spftradecontractwebimage/upload";
	}
	
	@RequestMapping("edit.do")
	public String edit(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "spftradecontractwebimage/edit";
	}
	
	@RequestMapping("add.do")
	public String add(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "spftradecontractwebimage/edit";
	}
	
	@RequestMapping("show.do")
	public String show(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("funcId", "spfTradeContractWebimage");
		request.setAttribute("contractId", request.getParameter("contractId"));
		return "spftradecontract/show";
	}
	
	@RequestMapping("wagelist.do")
	@ResponseBody
	public PageResultDto<SpfTradeWageDto> wagelist(SpfTradeWageDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String contractId = request.getParameter("contractId");
		if (StrUtil.isBlank(contractId)) {
			return new PageResultDto<SpfTradeWageDto>();
		}
		dto.setTradeContractId(new Integer(contractId));
		PageResultDto<SpfTradeWageDto> result = spfTradeWageService.getList(dto);
		return result;
	}
	
	@RequestMapping("saveFileAndInsertInfo.do")
	@ResponseBody
	public ResultDto saveFileAndInsertInfo(HttpServletRequest request, 
			MultipartHttpServletRequest request2) {
		String contractId = request.getParameter("contractId");
		if (StrUtil.isBlank(contractId)) {
			return error("请求参数错误");
		}
		// 上传文件
		MultipartFile file = request2.getFile("file");
		String targetName = FileTransportUtil.saveFile(this.picRealPath+"/" + contractId, file);
		// 保存记录
		SpfTradeContractWebimageDto dto = new SpfTradeContractWebimageDto();
		dto.setImgUrl("/" + contractId + "/" + targetName);
		dto.setTradeContractId(new Integer(contractId));
		warpModifyInfoByName(dto, request2, true);
		spfTradeContractWebimageService.insert(dto);
		dto.setImgUrl(this.picUrl + dto.getImgUrl());
//		String uploadIndex = request.getParameter("uploadIndex");
//		if (StrUtil.equals(uploadIndex, "1")) {
//			
//		}
		return success(dto);
	}
	
	@RequestMapping("submit.do")
	@ResponseBody
	public ResultDto submit(HttpServletRequest request, 
			HttpServletResponse response) {
		String contractId = request.getParameter("contractId");
		if (StrUtil.isBlank(contractId)) {
			return error("请求参数错误");
		}
		spfTradeContractWebimageService.submit(new Integer(contractId));
		String emails = systemConfigService.getConfigValue(
				PropUtil.use(Const.Config.CONFIG_FILE).getInt(Const.Config.SYSTEM_ID),
				"audit_remind_email");
		if (StrUtil.notBlank(emails)) {
			// 发送邮件
			String subject = "网签合同审核提醒";
			StringBuffer content = new StringBuffer();
			content.append(getLoginUser(request).getUserName());
			content.append("提交了网签合同照片，请<a href='");
			content.append(PropUtil.use("config.properties").get("clientLogoutUrl"));
			content.append("'>点击这里</a>登录系统进行审核。");
			Map<String, Object> map = new HashMap<>();
			map.put("content", content.toString());
			map.put("data", spfTradeContractService.getSpfTradeContract(new Integer(contractId)));
			String html = HtmlTemplateUtil.getHtml(map, FileUtil.getWebPath()+"/email_template", "contract");
			MailUtil.sendMail(emails, subject, html);
		}
		return success("提交成功");
	}
	
	@RequestMapping("list.do")
	@ResponseBody
	public PageResultDto<SpfTradeTrackFormDto> list(SpfTradeTrackFormFilterDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		FilterUtil.wrapFilterDto(dto, request);
		this.wrapDataAuth(dto);
		PageResultDto<SpfTradeTrackFormDto> result = spfTradeTrackFormService.getList(dto);
		return result;
	}
	
	@RequestMapping("imglist.do")
	@ResponseBody
	public ResultDto imglist(HttpServletRequest request, 
			HttpServletResponse response) {
		String contractId = request.getParameter("contractId");
		if (StrUtil.isBlank(contractId)) {
			return error("参数格式不正确");
		}
		List<SpfTradeContractWebimage> list = spfTradeContractWebimageService.getList(new Integer(contractId));
		for (SpfTradeContractWebimage spfTradeContractWebimage : list) {
			spfTradeContractWebimage.setImgUrl(this.picUrl + spfTradeContractWebimage.getImgUrl());
		}
		return success(list);
	}
	
	@RequestMapping("get.do")
	@ResponseBody
	public ResultDto getSpfTradeContractWebimage(SpfTradeContractWebimageDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		SpfTradeContractWebimageDto spfTradeContractWebimage = spfTradeContractWebimageService.getSpfTradeContractWebimage(dto.getId());
		return success(spfTradeContractWebimage);
	}
	
	@RequestMapping("getDetail.do")
	@ResponseBody
	public ResultDto getContractDetail(HttpServletRequest request, 
			HttpServletResponse response) {
		String id = request.getParameter("id");
		if (StrUtil.isBlank(id)) {
			return error("请求参数错误");
		}
		SpfTradeContractDetailDto dto = spfTradeContractService.getContractDetail(new Integer(id));
		return success(dto);
	}
	
	@RequestMapping("del.do")
	@ResponseBody
	public ResultDto del(SpfTradeContractWebimageDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		SpfTradeContractWebimageDto spfTradeContractWebimageDto = spfTradeContractWebimageService.getSpfTradeContractWebimage(dto.getId());
		if (!StrUtil.equals(spfTradeContractWebimageDto.getCreateBy(), this.getLoginUser(request).getUserName())) {
			return error("当前数据不属于您，不能编辑");
		}
		String msg = spfTradeContractWebimageService.del(dto.getId());
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("删除成功");
	}
}
